package com.cskaoyan.market.admin.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.cskaoyan.market.db.domain.MarketIssue;
import com.cskaoyan.market.db.domain.MarketIssueExample;
import com.cskaoyan.market.db.mapper.MarketIssueMapper;
import com.cskaoyan.market.admin.service.MarketIssueService;
import com.cskaoyan.market.util.MyBatisUtil;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.session.SqlSession;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class MarketIssueServiceImpl implements MarketIssueService {
    @Autowired
    private MarketIssueMapper issueMapper;

    @Override
    public List<MarketIssue> list(String question, int page, int limit, String sort, String order) {

        
        
        MarketIssueExample marketIssueExample = new MarketIssueExample();
        marketIssueExample.orderBy(sort + " " + order).or().andDeletedEqualTo(false);
        PageHelper.startPage(page, limit);
        List<MarketIssue> marketIssues = issueMapper.selectByExample(marketIssueExample);
        
        
        return marketIssues;
    }

    @Override
    public MarketIssue create(String answer, String question) {

        
        
        MarketIssue marketIssue = new MarketIssue();
        marketIssue.setQuestion(question);
        marketIssue.setAnswer(answer);
        LocalDateTime dateTime = LocalDateTime.now();
        marketIssue.setAddTime(dateTime);
        marketIssue.setUpdateTime(dateTime);
        marketIssue.setDeleted(false);
        issueMapper.insert(marketIssue);
        
        
        return marketIssue;
    }

    @Override
    public MarketIssue update(int id, String answer, String question) {

        
        
        MarketIssueExample marketIssueExample = new MarketIssueExample();
        marketIssueExample.or().andIdEqualTo(id);
        MarketIssue marketIssue = issueMapper.selectByExample(marketIssueExample).get(0);
        LocalDateTime dateTime = LocalDateTime.now();
        marketIssue.setUpdateTime(dateTime);
        marketIssue.setQuestion(question);
        marketIssue.setAnswer(answer);
        issueMapper.updateByExample(marketIssue, marketIssueExample);
        
        
        return marketIssue;
    }

    @Override
    public void delete(int id) {

        
        
        MarketIssueExample marketIssueExample = new MarketIssueExample();
        marketIssueExample.or().andIdEqualTo(id);
        issueMapper.logicalDeleteByExample(marketIssueExample);
        
        
    }
}
